#include "foreignkeyconstraint.h"

#include "columnlist.h"
#include "column.h"

ForeignKeyConstraint::ForeignKeyConstraint(ColumnList * table_columns, ColumnList * referenced_columns, const string & referenced_table)
{
    if(table_columns == NULL) this->table_columns = NULL;
    else this->table_columns = table_columns->clone();

    if(referenced_columns == NULL) this->referenced_columns = NULL;
    else this->referenced_columns = referenced_columns->clone();

    this->referenced_table = referenced_table;
}

ForeignKeyConstraint::~ForeignKeyConstraint()
{
    delete table_columns; table_columns = NULL;
    delete referenced_columns; referenced_columns = NULL;
    referenced_table = "";
}

ForeignKeyConstraint::ForeignKeyConstraint(const ForeignKeyConstraint & other)
{
    if(other.table_columns == NULL) table_columns = NULL;
    else table_columns = other.table_columns->clone();

    if(other.referenced_columns == NULL) referenced_columns = NULL;
    else referenced_columns = other.referenced_columns->clone();

    referenced_table = other.referenced_table;
}

void ForeignKeyConstraint::setTable(const string & table)
{
    this->referenced_table = table;
}

void ForeignKeyConstraint::setReferencedColumns(ColumnList * columns)
{
    delete referenced_columns; referenced_columns = NULL;
    referenced_columns = columns->clone();
}

void ForeignKeyConstraint::setTableColumns(ColumnList * columns)
{
    delete table_columns; table_columns = NULL;
    table_columns = columns->clone();
}

string ForeignKeyConstraint::getTable()
{
    return referenced_table;
}

bool ForeignKeyConstraint::getReferencedColumn(const string & column, string & referenced)
{
    if(table_columns == NULL || referenced_columns == NULL) return false;

    // Search the column in the table_columns ColumnList to get the index.
    vector<Column *> * tb = table_columns->getColumns();
    unsigned int i=0;
    bool found = false;
    while(i<tb->size() && !found){
        if(tb->at(i)->getName() == column) found = true;
        i++;
    }//while

    if (!found) return false;

    // Get the referenced with the index of the column.
    unsigned int index = i-1;
    vector<Column *> * tr = referenced_columns->getColumns();
    if(index >= tr->size()) return false;

    referenced = tr->at(index)->getName();
    return true;
}

string ForeignKeyConstraint::toString()
{
    string str = "[FOREIGN KEY: ";
    if(table_columns != NULL){
        str = str + table_columns->toString();
    }//if
    str = str + " REFERENCES ";
    if(referenced_columns != NULL){
        str = str + referenced_columns->toString();
    }//if
    str = str + " OF " + referenced_table + "]";
    return str;
}

TableConstraint* ForeignKeyConstraint::clone()
{
    return new ForeignKeyConstraint(*this);
}

void ForeignKeyConstraint::add(const string & column, const string & referenced)
{
    if(table_columns == NULL) table_columns = new ColumnList();
    table_columns->addColumn(column);

    if(referenced_columns == NULL) referenced_columns = new ColumnList();
    referenced_columns->addColumn(referenced);
}

void ForeignKeyConstraint::add(Column * column, Column * referenced)
{
    if(table_columns == NULL) table_columns = new ColumnList();
    table_columns->addColumn(column);

    if(referenced_columns == NULL) referenced_columns = new ColumnList();
    referenced_columns->addColumn(referenced);
}



